package com.myjob.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopgoods.common.core.domain.R;
import com.shopgoods.system.api.RemoteFileService;
import com.shopgoods.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.myjob.system.mapper.SgTypeMapper;
import com.myjob.system.domain.SgType;
import com.myjob.system.service.ISgTypeService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 品牌Service业务层处理
 *
 * @author shopgoods
 * @date 2024-11-12
 */
@Service
public class SgTypeServiceImpl extends ServiceImpl<SgTypeMapper, SgType> implements ISgTypeService
{



    @Autowired
    private SgTypeMapper sgTypeMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询品牌
     *
     * @param typeId 品牌主键
     * @return 品牌
     */
    @Override
    public SgType selectSgTypeByTypeId(Long typeId)
    {
        return sgTypeMapper.selectById(typeId);
    }

    /**
     * 查询品牌列表
     *
     * @param sgType 品牌
     * @return 品牌
     */
    @Override
    public List<SgType> selectSgTypeList(SgType sgType)
    {
        QueryWrapper<SgType> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(sgType.getTypeName())){
            queryWrapper.like("type_name",sgType.getTypeName());
        }
        return this.list(queryWrapper);
    }

    /**
     * 新增品牌
     *
     * @param sgType 品牌
     * @return 结果
     */
    @Override
    public int insertSgType(SgType sgType)
    {
        return sgTypeMapper.insert(sgType);
    }

    /**
     * 修改品牌
     *
     * @param sgType 品牌
     * @return 结果
     */
    @Override
    @Async
    public int updateSgType(SgType sgType)
    {
        return sgTypeMapper.update(sgType, null);
    }

    /**
     * 批量删除品牌
     *
     * @param typeIds 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deleteSgTypeByTypeIds(Long[] typeIds)
    {
        return sgTypeMapper.deleteBatchIds(Arrays.asList(typeIds));
    }

    /**
     * 删除品牌信息
     *
     * @param typeId 品牌主键
     * @return 结果
     */
    @Override
    public int deleteSgTypeByTypeId(Long typeId)
    {
        return sgTypeMapper.deleteById(typeId);
    }

    @Override
    public R<SysFile> avatar(MultipartFile file) {

        if (!file.isEmpty()){
            R<SysFile> upload = remoteFileService.upload(file);
            return upload;
        }
        return new R<>().fail("上传图片异常，请联系管理员");
    }

    /**
     * 修改品牌是否启用
     *
     * @param sgType
     * @return
     */
    @Override
    public int updateSgTypeDisplay(SgType sgType) {
        return sgTypeMapper.updateById(sgType);
    }

    /**
     * 修改品牌制造商是否启用
     *
     * @param sgType
     * @return
     */
    @Override
    public int updateTypeMakerData(SgType sgType) {
        return sgTypeMapper.update(sgType, null);
    }
}
